#encoding=utf-8
import traceback

from common.CaseCheck import verify_yaml, CaseCheckInfo
from common.extract_util import ExtractUtil
from common.requests_until import RequestsUntil, logger
from common.assert_util import AssertUtil

eu = ExtractUtil()
ru = RequestsUntil()
au = AssertUtil()

def stand_case_utl(caseinfo: CaseCheckInfo):
    # 把请求参数写入日志
    logger.info("模块>接口>用例: " + str(caseinfo.feature) + ">" + str(caseinfo.story) + ">" + str(caseinfo.title))
    # 替换其中的参数值
    new_request = eu.change(caseinfo.request)

    res = ru.send_request(**new_request)
    # 请求之后去提取变量
    if caseinfo.extract:
        for key, value in caseinfo.extract.items():
            # 提取响应中的值
            eu.extract(res, key, *value)

    # 响应之后的断言 validate为空则不需要校验
    try:
        if caseinfo.validate:
            for assert_type, value in eu.change(caseinfo.validate).items():
                # print("断言类型：%s,断言值：%s"%(assert_type,value))
                # 进行断言判断
                au.assert_util(res, assert_type, value)
            logger.info("用例断言成功！\n")
        else:
            logger.warning("用例没有断言!\n")
    except Exception as e:
        logger.error("用例断言失败，%s"% str(traceback.format_exc()))
        raise e
